package com.chen.controller;

import com.chen.model.Contract;
import com.chen.model.Customer;
import com.chen.model.Goods;
import com.chen.model.Invoice;
import com.chen.service.ContractService;
import com.chen.service.CustomerService;
import com.chen.service.GoodsService;
import com.chen.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/findAllStatisticsTotal")
    public String findAllStatisticsTotal(Model model , HttpServletRequest request) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        String date1 = request.getParameter("date1");
        String date2 = request.getParameter("date2");
        List<Contract> contractList = contractService.findAllContract();
        List<Integer> totalList = new ArrayList<Integer>();
//        if(!date1.equals("")&&!date2.equals("")) {
//        Date utilDate = sdf.parse(time1);
        if(date1==null||date2==null){
            for (Contract contract : contractList) {
                List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                Integer total = 0;
                for (Invoice invoice : invoiceList) {
                    total += invoice.getGoods_number() * invoice.getGoods_price();
                    System.out.println(total);
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            Integer contract_total = 0;
            for (Integer integer : totalList) {
                contract_total += integer;
            }
            model.addAttribute("contract_total", contract_total);
            model.addAttribute("contractList", contractList);
            model.addAttribute("totalList", totalList);
        }
        else {
            Date time1 = sdf.parse(date1);
            Date time2 = sdf.parse(date2);
            List<Contract> contractList1 = new ArrayList<Contract>();
            for (Contract contract:contractList){
                if(!contract.getContract_time().before(time1)&&!contract.getContract_time().after(time2)){
                    contractList1.add(contract);
                }
            }
            for (Contract contract : contractList1) {
                List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                Integer total = 0;
                for (Invoice invoice : invoiceList) {
                    total += invoice.getGoods_number() * invoice.getGoods_price();
                    System.out.println(total);
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            Integer contract_total = 0;
            for (Integer integer : totalList) {
                contract_total += integer;
            }
            model.addAttribute("contract_total", contract_total);
            model.addAttribute("contractList", contractList1);
            model.addAttribute("totalList", totalList);
        }
        return "statistics-total";
    }

    @RequestMapping("/statistics_total")
    public String StatisticsTotal(){
        return "statistics-total";
    }


    @RequestMapping("/findAllStatisticsCustomer")
    public String findAllStatisticsCustomer(Model model , HttpServletRequest request) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        String date1 = request.getParameter("date1");
        String date2 = request.getParameter("date2");
        List<Contract> contractList = contractService.findAllContract();
        List<Integer> totalList = new ArrayList<Integer>();
        List<Integer> totals = new ArrayList<>();
        List<Customer> customerList;
        customerList = customerService.findAllCustomer();
        if(date1==null||date2==null){
            for (Contract contract : contractList) {
                List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                Integer total = 0;
                for (Invoice invoice : invoiceList) {
                    total += invoice.getGoods_number() * invoice.getGoods_price();
                    System.out.println(total);
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            for (Customer customer:customerList){
                Integer total = 0;
                for (int i=0;i<contractList.size();i++){
                    if(contractList.get(i).getCustomer().getCustomer_id()==customer.getCustomer_id()){
                        total += totalList.get(i);
                    }
                }
                totals.add(total);
            }
            model.addAttribute("customerList", customerList);
            model.addAttribute("totals", totals);
        }
        else {
            Date time1 = sdf.parse(date1);
            Date time2 = sdf.parse(date2);
            List<Contract> contractList1 = new ArrayList<Contract>();
            for (Contract contract:contractList){
                if(!contract.getContract_time().before(time1)&&!contract.getContract_time().after(time2)){
                    contractList1.add(contract);
                }
            }
            for (Contract contract : contractList1) {
                List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                Integer total = 0;
                for (Invoice invoice : invoiceList) {
                    total += invoice.getGoods_number() * invoice.getGoods_price();
                    System.out.println(total);
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            for (Customer customer:customerList){
                Integer total = 0;
                for (int i=0;i<contractList1.size();i++){
                    if(contractList1.get(i).getCustomer().getCustomer_id()==customer.getCustomer_id()){
                        total += totalList.get(i);
                    }
                }
                totals.add(total);
            }
            model.addAttribute("customerList", customerList);
            model.addAttribute("totals", totals);
        }
        return "statistics-customer";
    }

    @RequestMapping("/findAllStatisticsGoods")
    public String findAllStatisticsGoods(Model model , HttpServletRequest request) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        String date1 = request.getParameter("date1");
        String date2 = request.getParameter("date2");
        List<Contract> contractList = contractService.findAllContract();
        List<Integer> totalList = new ArrayList<Integer>();
        List<Integer> totals = new ArrayList<>();
        List<Goods> goodsList;
        goodsList = goodsService.findAllGoods();
        if(date1==null||date2==null){
            for (Goods goods:goodsList){
                Integer total = 0;
                for (Contract contract : contractList) {
                    List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                    for (Invoice invoice : invoiceList) {
                        if(invoice.getGoods().getGoods_id()==goods.getGoods_id()){
                            total += invoice.getGoods_number() * invoice.getGoods_price();
                            System.out.println(total);
                        }
                    }
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            model.addAttribute("goodsList", goodsList);
            model.addAttribute("totalList", totalList);
        }
        else {
            Date time1 = sdf.parse(date1);
            Date time2 = sdf.parse(date2);
            List<Contract> contractList1 = new ArrayList<Contract>();
            for (Contract contract:contractList){
                if(!contract.getContract_time().before(time1)&&!contract.getContract_time().after(time2)){
                    contractList1.add(contract);
                }
            }
            for (Goods goods:goodsList){
                Integer total = 0;
                for (Contract contract : contractList1) {
                    List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                    for (Invoice invoice : invoiceList) {
                        if(invoice.getGoods().getGoods_id()==goods.getGoods_id()){
                            total += invoice.getGoods_number() * invoice.getGoods_price();
                            System.out.println(total);
                        }
                    }
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            model.addAttribute("goodsList", goodsList);
            model.addAttribute("totalList", totalList);
        }
        return "statistics-goods";
    }
}
